package com.standardoperationapp.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.standardoperationapp.R;
import com.standardoperationapp.bean.CardPointBean;
import com.standardoperationapp.bean.CheckNumBean;

import java.util.List;

public class CheckNumListAdapter extends BaseAdapter {
    private List<CheckNumBean.DataBean> list;
    private LayoutInflater mInflater;

    private int selectedEditTextPosition = -1;

    public CheckNumListAdapter(Context context, List<CheckNumBean.DataBean> list1) {
        list = list1;
        mInflater = LayoutInflater.from(context);
    }



    public int getCount() {
        return list.size();
    }

    public CheckNumBean.DataBean getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public int getItemViewType(int position) {
        return position;
    }

    @SuppressLint("ClickableViewAccessibility")
    public View getView(int position, View convertView, ViewGroup parent)
    {
        CheckNumBean.DataBean item = list.get(position);
        ViewHolder viewHolder = null;

        if(convertView == null){
            convertView = mInflater.inflate(R.layout.item_check_num, null);
            viewHolder= new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.tvName.setText(item.getToolsName());

        viewHolder.etCount.setOnTouchListener((view, motionEvent) -> {
            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                EditText editText = (EditText) view;
                selectedEditTextPosition = (int) editText.getTag();
            }
            return false;
        }); // 正确写法
        viewHolder.etCount.setOnFocusChangeListener((view, hasFocus) -> {
            EditText editText = (EditText) view;
            if (hasFocus) {
                editText.addTextChangedListener(mTextWatcher);
            } else {
                editText.removeTextChangedListener(mTextWatcher);
            }
        });
        viewHolder.etCount.setTag(position);

        if (selectedEditTextPosition != -1 && position == selectedEditTextPosition) { // 保证每个时刻只有一个EditText能获取到焦点
            viewHolder.etCount.requestFocus();
        } else {
            viewHolder.etCount.clearFocus();
        }
        String text = list.get(position).getToolsNum() + "";
        viewHolder.etCount.setText(text);
        viewHolder.etCount.setSelection(viewHolder.etCount.length());

        return convertView;
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (selectedEditTextPosition != -1) {
                Log.w("MyEditAdapter", "onTextPosiotion " + selectedEditTextPosition);
                CheckNumBean.DataBean itemTest = getItem(selectedEditTextPosition);
                if (s.toString().isEmpty()) {
                    itemTest.setToolsNum(0);
                }else {
                    itemTest.setToolsNum(Integer.parseInt(s.toString()));
                }
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    class ViewHolder {
        protected TextView tvName;
        protected EditText etCount;
        protected LinearLayout llContent;

        public ViewHolder(View convertView){
            this.tvName =  convertView.findViewById(R.id.tv_name);
            this.etCount =  convertView.findViewById(R.id.et_count);
            this.llContent =  convertView.findViewById(R.id.ll_content);
        }
    }
}
